<!--
 * @Description:缓动基本函数封装多个
 * @Author: Sun
 * @Date: 2019-07-01 20:05:50
 * @LastEditors: Sun
 * @LastEditTime: 2019-07-26 22:22:33
 -->
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>缓动基本函数封装多个</title>
	<style>
		* {
			margin: 0;
			padding: 0
		}

		button {
			border: 1px solid eee;
			padding: 5px 10px;
		}

		div {
			width: 100px;
			height: 100px;
			background-color: pink;
			position: absolute;
			left: 0px;
			top: 50px;
		}
	</style>
</head>

<body>
	<button id="btn200"> 200</button>
	<button id="btn400">400</button>
	<div id="box">点击按钮</div>
	<script>
		function $(id) {
			return document.getElementById(id);
		}
		window.onload = function () {
			var btn200 = $("btn200")
			var btn400 = $("btn400")
			var timer = null
			var box = $("box")
			btn200.onclick = function () {
				animate(box,{width:200,left:200,top: 200,opacity:50,zIndex:5}, 500)
			}
			btn400.onclick = function () {
				animate(box, {top: 80,width:80}, 400)
			}
			//给谁 json
			function animate(obj,json) {
				clearInterval(obj.timer)
				obj.timer = setInterval(() => {
				var flag=true //用来判断是否停止定时器
				//计算步长 动画原理 盒子本身样式+步长
				// //我们怎么知道我们当前的样式
				// //当前样式 （target-当前样式）/10==步长
				for (const key in json) {
					  var current=0;
						if(key=="opacity"){
							current=parseInt(getStyle(obj, "", key)*100)
						}else{
							current=parseInt(getStyle(obj, "", key))
						}
						// current = parseInt(getStyle(obj, "", key)*100);
						var step = (json[key] - current) / 10; //步长
						step = step > 0 ? Math.ceil(step) : Math.floor(step) //取整步长
						
						//透明度
						if(key=="opacity"){
							if("opacity" in obj.style){
								obj.style.opacity=(current+step)/100
							}else{
								obj.style.filter="alpha(opacity="+(current+step)+")"
							}	
						}else if(key=="zIndex"){
							obj.style.zIndex=json[key]
						}else{
							obj.style[key]=current + step + "px"
						}

						obj.style[key] = current + step + "px"
						//只要其中不满足条件 就不停止定时器
						if(current!=json[key]){
								flag=false
						}
				}

				if(flag){//用于判断定时器的条件
					clearInterval(obj.timer)
				}
					
				}, 60)
			}



			//封装多个属性的运动属性
			//封装函数 1 动谁 2什么属性 3 动多少
			// function animate(obj, attr, target) {
			// 	clearInterval(obj.timer)
			// 	obj.timer = setInterval(() => {
			// 		//计算步长  动画原理 盒子本身样式+步长
			// 		//我们怎么知道我们当前的样式
			// 		//当前样式 （target-当前样式）/10==步长
			// 		var current = parseInt(getStyle(obj, "", attr));
			// 		console.log("sv", current)
			// 		var step = (target - current) / 10; //步长
			// 		step = step > 0 ? Math.ceil(step) : Math.floor(step) //取整步长
			// 		obj.style[attr] = current + step + "px"
			// 		if (current == target) {
			// 			clearInterval(obj.timer)
			// 		}
			// 	}, 30)
			// }

			function getStyle(ele, tum = null, attr) {
				if (window.getComputedStyle) { // 元素 伪类 css名字
					return window.getComputedStyle(ele, tum)[attr];
				}
				return ele.currentStyle[attr]; //ie
			}


		}
	</script>

</body>

</html>